package com.beordie.controller;

import com.beordie.model.Category;
import com.beordie.model.Image;
import com.beordie.model.Route;
import com.beordie.model.Seller;
import com.beordie.service.ICategoryService;
import com.beordie.service.IImageService;
import com.beordie.service.IRouteService;
import com.beordie.service.ISellerService;
import com.beordie.utils.UploadFileUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description 分类管理控制层
 * @Date 2021/8/4 20:00
 * @Created 30500
 */
@RequestMapping("route")
@Controller
public class RouteController {
    @Resource
    private IRouteService routeServiceImpl;
    @Resource
    private ICategoryService categoryServiceImpl;
    @Resource
    private ISellerService sellerServiceImpl;
    @Resource
    private IImageService imageServiceImpl;

    @RequestMapping(value = "list")
    public String pageInfo(@RequestParam(defaultValue = "1") int pageNum,
                           @RequestParam(defaultValue = "5") int pageSize,
                           Route route, Model model) {

        List<Category> categoryList = categoryServiceImpl.find();
        List<Seller> sellerList = sellerServiceImpl.find();
        PageInfo<Route> pageInfo = routeServiceImpl.findPage(route, pageNum, pageSize);

        model.addAttribute("category", categoryList);
        model.addAttribute("seller", sellerList);
        model.addAttribute("page", pageInfo);
        model.addAttribute("categoryId", route.getCategoryId());
        model.addAttribute("sellerId", route.getSellerId());
        return "/route/list";
    }

    @RequestMapping(value = "toUpdate/{id}", method = RequestMethod.GET)
    public String tpUpdate(@PathVariable("id") int id, Model model) {
        Route route = routeServiceImpl.findById(id);
        List<Category> categoryList = categoryServiceImpl.find();
        List<Seller> sellerList = sellerServiceImpl.find();
        model.addAttribute("category", categoryList);
        model.addAttribute("seller", sellerList);
        model.addAttribute("route", route);
        return "/route/update";
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String update(@RequestParam("file") MultipartFile file, int id, Route route) throws IOException {
        String fileName = file.getOriginalFilename();
        if (!route.getImage().equals(fileName)) {
            String newName = UploadFileUtils.getFileName(1, file);
            route.setImage(newName);
        }
        route.setDate(new Date());
        route.setId(id);
        int update = routeServiceImpl.update(route);
        return "redirect:/route/list";
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model) {
        List<Category> categoryList = categoryServiceImpl.find();
        List<Seller> sellerList = sellerServiceImpl.find();
        model.addAttribute("category", categoryList);
        model.addAttribute("seller", sellerList);

        return "/route/add";
    }

    @RequestMapping("addImage")
    public String addImage(int id,
                           @RequestParam("big") MultipartFile[] bigPicFile,
                           @RequestParam("small")MultipartFile[] smallPicFile) throws IOException {
        List<String> bigNames = new ArrayList<>();
        List<String> smallNames = new ArrayList<>();
        for (int i = 0; i < bigPicFile.length; i++) {
            String bigName = UploadFileUtils.getFileName(2, bigPicFile[i]);
            String smallName = UploadFileUtils.getFileName(3, smallPicFile[i]);
            bigNames.add(bigName);
            smallNames.add(smallName);
        }
        int add = imageServiceImpl.add(id, smallNames, bigNames);
        return "redirect:/route/list";
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String add(@RequestParam("file") MultipartFile file, Route route) throws IOException {
        String fileName = UploadFileUtils.getFileName(1, file);
        route.setImage(fileName);
        route.setDate(new Date());
        routeServiceImpl.add(route);
        return "redirect:/route/list";
    }

    @RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
    public String delete(@PathVariable("id") int id) {
        int delete = routeServiceImpl.delete(id);
        return "redirect:/route/list";
    }

    @RequestMapping(value = "toImage/{id}", method = RequestMethod.GET)
    public String toImage(@PathVariable("id") int id, Model model) {
        List<Image> imageList = imageServiceImpl.findByRouteId(id);
        model.addAttribute("id", id);
        model.addAttribute("images", imageList);
        return "/route/image";
    }

}
